//
//  SNLandInformationCell.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/29.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNCustomAlertView.h"
#import "SNLandInformationCell.h"
#import "CustomIOSAlertView.h"
@interface SNLandInformationCell()<CustomIOSAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *selectedButton;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
/** 可用面积 */
@property (weak, nonatomic) IBOutlet UILabel *usableAreaLabel;
/** 实际占用 */
@property (weak, nonatomic) IBOutlet UILabel *occupancyLabel;

@end
@implementation SNLandInformationCell
#pragma mark - <CustomIOSAlertViewDelegate>
- (void)customIOS7dialogButtonTouchUpInside: (CustomIOSAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex
{
    NSLog(@"Delegate: Button at position %d is clicked on alertView %d.", (int)buttonIndex, (int)[alertView tag]);
    SNCustomAlertView * demoView = (SNCustomAlertView *)alertView.containerView;
    
    NSString * inputString  = demoView.textField.text;
    
    if (inputString.integerValue > self.landReqult.availableArea.integerValue) {
        
        demoView.warningLabel.hidden = NO;
        
        demoView.warningLabel.text = [NSString stringWithFormat:@"可使用面积为%@公顷, 已超过, 请重新输入",self.landReqult.availableArea];
        
    }else{
        self.occupancyLabel.text = demoView.textField.text;
        demoView.warningLabel.hidden = YES;
        self.landReqult.plantArea = demoView.textField.text;
        [alertView close];
    }
    
}
- (UIView *)createDemoView
{
    SNCustomAlertView * demoView =   [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([SNCustomAlertView class]) owner:nil options:nil] lastObject];
    
    demoView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width-50, 93);
    
    return demoView;
}
- (IBAction)selectButtonClicked:(UIButton *)sender {
    NSLog(@"%@ | %s",self.class,__func__);
    sender.selected = !sender.selected;
    self.landReqult.selected = sender.selected;
    
}
- (IBAction)editButtonClicked:(UIButton *)sender {
    NSLog(@"%@ | %s",self.class,__func__);
    CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
    
    // Add some custom content to the alert view
    [alertView setContainerView:[self createDemoView]];
    
    // Modify the parameters
    [alertView setButtonTitles:[NSMutableArray arrayWithObjects:@"确定", nil]];
    [alertView setDelegate:self];
    
//    // You may use a Block, rather than a delegate.
//    [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
//        NSLog(@"Block: Button at position %d is clicked on alertView %d.", buttonIndex, (int)[alertView tag]);
//        [alertView close];
//    }];
    
    [alertView setUseMotionEffects:true];
    
    // And launch the dialog
    [alertView show];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
-(void)setLandReqult:(SNLandResult *)landReqult{
    _landReqult = landReqult;
    
//    if(YES){
//        self.selectedIconImageView.image = [UIImage imageNamed:@"06-earth-selected"];
//    }else{
//        self.selectedIconImageView.image = [UIImage imageNamed:@"06-earth-unselected"];
//    }
    self.selectedButton.selected = landReqult.selected;
    
    self.titleLabel.text = [NSString stringWithFormat:@"%@ | %@",landReqult.landLocation,landReqult.landTypeName];
    self.usableAreaLabel.text = landReqult.availableArea;
    self.occupancyLabel.text = (landReqult.plantArea.length > 0) ? landReqult.plantArea : landReqult.availableArea;
    landReqult.plantArea = self.occupancyLabel.text;
}
-(void)setModel:(NSDictionary *)model{
    _model = model;
    
//    if([model[@"selected"] boolValue]){
//        self.selectedIconImageView.image = [UIImage imageNamed:@"06-earth-selected"];
//    }else{
//        self.selectedIconImageView.image = [UIImage imageNamed:@"06-earth-unselected"];
//    }
    
    self.titleLabel.text = model[@"title"];
    self.usableAreaLabel.text = [NSString stringWithFormat:@"%@",model[@"usableArea"]];
    self.occupancyLabel.text = [NSString stringWithFormat:@"%@",model[@"occupancy"]];
    
}

@end
